<script setup lang='ts'>
import type { PlusColumn } from 'plus-pro-components'

import { useAagreementMetaAudit } from '@fl/api/purchase'
import { SubmitBtn } from '@fl/components'

const { isPending: AagreementMetaAuditLoading, mutate: AagreementMetaAuditMutate } = useAagreementMetaAudit()
const visible = defineModel<boolean>('visible')
const id = defineModel('id')
const values = ref({
    sendFlag: 1,
})

const columns: PlusColumn[] = [
    {
        label: '是否发送短信',
        labelWidth: '200',
        options: [

            {
                label: '发送',
                value: 1,
            },
            {
                label: '不发送',
                value: 2,
            },
        ],
        prop: 'sendFlag',
        valueType: 'radio',
    },
]
const rules = {
    sendFlag: [
        {
            message: '是否向供应商(含经销商)发送协议、订单通知短信',
            required: true,
        },
    ],
}
const formRef = ref()

async function confirm() {
    const valid = await formRef.value.formInstance.validate()
    if (valid) {
        const params = {
            id: String(id.value),
            sendFlag: Number(values.value.sendFlag),
            status: 1,
        }
        AagreementMetaAuditMutate(params, {
            onSuccess: () => {
                ElMessage.success('操作成功')
                cancel()
            },
        })
    }
}

function cancel() {
    visible.value = false
}

function reset() {
    formRef.value.formInstance.resetFields()
}
</script>

<template>
    <PlusDialog v-model="visible"
                title="审核通过"
                width="800"
                close-on-press-escape
                @cancel="cancel"
                @closed="reset"
    >
        <PlusForm ref="formRef"
                  v-model="values"
                  :columns="columns"
                  :label-width="100"
                  :rules="rules"
                  :has-footer="false"
        />

        <template #footer>
            <div class="mx-auto">
                <el-button @click="cancel">
                    取消
                </el-button>

                <SubmitBtn type="primary"
                           :loading="AagreementMetaAuditLoading"
                           @click="confirm"
                >
                    提交
                </SubmitBtn>
            </div>
        </template>
    </PlusDialog>
</template>
